//
//  ForgetPwdViewController.m
//  TengYunWaystation
//
//  Created by Johnny on 15/7/15.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "SumitPwdViewController.h"
#import "DSToast.h"
#import "TSHeader.h"
#import "SocketRequest.h"
#import "AFHttpService.h"

@interface ForgetPwdViewController ()<UITextFieldDelegate,SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation ForgetPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"输入手机号";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _inputView.layer.borderColor = LineColor.CGColor;
    _inputView.layer.borderWidth = 0.5;
    [_phoneTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [_phoneTextField becomeFirstResponder];
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    if (textField.text.length >= 11) {
        textField.text = [textField.text substringToIndex:11];
    }
}

- (void)checkUserPhoneIsLeagelSocket {
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STUA"]];
    [mutableData appendBytes:[SocketMethod checkLegal:_phoneTextField.text] length:16];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

- (void)getUserModel {
    [ToastActivity makeActivity:@"加载中..." InView:self.view];
    [[AFHttpService shareInstance] POST:@"/dispatchplatform/getSiteUserInfo" Dictionary:@{@"userTel":CheckString(_phoneTextField.text)} success:^(ResponseHeader *response) {
        if (response.code == 0) {
            CityModel *cityModel = [CityModel objectWithKeyValues:response.content];
            [Singleton shareInstance].cityModel = cityModel;
            [self checkUserPhoneIsLeagelSocket];
        } else {
            [ToastActivity makeError:response.msg InView:self.view];
        }
    } failure:^(NSString *errorMsg) {
        [ToastActivity makeError:errorMsg InView:self.view];
    }];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        [ToastActivity dismissInView:self.view];
        SumitPwdViewController *sumitPVC = [[SumitPwdViewController alloc] initWithNibName:@"SumitPwdViewController" bundle:nil];
        sumitPVC.phone = _phoneTextField.text;
        [self.navigationController pushViewController:sumitPVC animated:YES];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text.length == 11) {
        [self checkUserPhoneIsLeagelSocket];
    }
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:TelephoneLimit] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    if (![string isEqualToString:filtered]) {
        return NO;
    }
    return YES;
}

- (IBAction)sureAction:(id)sender {
    [_phoneTextField resignFirstResponder];
    if (_phoneTextField.text.length <= 0) {
        [DSToast showText:@"请输入手机号" InView:self.view];
    } else if (_phoneTextField.text.length != 11 || ![_phoneTextField.text hasPrefix:@"1"]) {
        [DSToast showText:@"请输入正确的11位手机号" InView:self.view];
    }  else {
        [self getUserModel];
    }
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
